package com.seasugar.raggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seasugar.raggie.common.R;
import com.seasugar.raggie.model.entity.ShoppingCart;
import com.seasugar.raggie.model.entity.User;
import com.seasugar.raggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Caching(evict = {
            @CacheEvict(value = "setmealCache", allEntries = true),
            @CacheEvict(value = "dishCache", allEntries = true),
            @CacheEvict(value = "shoppingCartCache", allEntries = true)
    })
    @PostMapping("add")
    public R getShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        //如果存在数量加一，不存在就加入购物车表
        if (shoppingCart != null) {
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart shoppingcart = shoppingCartService.getOne(queryWrapper);
            if (shoppingcart != null) {
                if (shoppingcart.getDishFlavor() != shoppingCart.getDishFlavor()) {
                    User user = (User) session.getAttribute("user");
                    shoppingCart.setUserId(user.getId());
                    shoppingCartService.save(shoppingCart);
                } else {
                    shoppingcart.setNumber(shoppingcart.getNumber() + 1);
                    shoppingCartService.updateById(shoppingcart);
                }
                return R.success(shoppingCart);
            }
            queryWrapper = new LambdaQueryWrapper<ShoppingCart>();
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            if (shoppingCart1 != null) {
                if (shoppingCart1.getDishFlavor() != shoppingCart.getDishFlavor()) {
                    User user = (User) session.getAttribute("user");
                    shoppingCart.setUserId(user.getId());
                    shoppingCartService.save(shoppingCart);
                } else {
                    shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
                    shoppingCartService.updateById(shoppingCart1);
                }
                return R.success(shoppingCart1);
            }
            //都不存在
            User user = (User) session.getAttribute("user");
            shoppingCart.setUserId(user.getId());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
        return R.error("添加失败");
    }

    @Caching(evict = {
            @CacheEvict(value = "setmealCache", allEntries = true),
            @CacheEvict(value = "dishCache", allEntries = true),
            @CacheEvict(value = "shoppingCartCache", allEntries = true)
    })
    @PostMapping("sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne != null) {
            Integer number = cartServiceOne.getNumber();
            if (number == 1) {
                shoppingCartService.remove(queryWrapper);
                return R.success(0);
            } else {
                cartServiceOne.setNumber(number - 1);
                //更新数据库
                shoppingCartService.updateById(cartServiceOne);
            }
            return R.success(cartServiceOne);
        }
        return R.error("失败");
    }


    @Cacheable(value = "shoppingCartCache", key = "'shoppingCart:'+#root.methodName")
    @GetMapping("list")
    public R getList() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.list();
        log.info(shoppingCarts.toString());
        if (shoppingCarts != null) {
            return R.success(shoppingCarts);
        }
        return R.error("当前购物车为空");
    }

    @Caching(evict = {
            @CacheEvict(value = "setmealCache", allEntries = true),
            @CacheEvict(value = "dishCache", allEntries = true),
            @CacheEvict(value = "shoppingCartCache", allEntries = true)
    })
    @DeleteMapping("clean")
    public R cleanShoppingCart(HttpSession session) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        User user = (User) session.getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId, user.getId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清除成功");
    }

}
